﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.Model;
using System.Configuration;
using System.ServiceModel;
using CMS.Contracts;


namespace Presentation
{
 
    public partial class TeacherList : System.Web.UI.Page
    {
        private IProjectBService _channelObject;
        private ChannelFactory<IProjectBService> _ChannelFactory = new ChannelFactory<IProjectBService>("CMSClient");

        private int _pageSize = 5;
        private static int _pageIndex = 0;
        private static int _teachersNumber = 0;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                _channelObject = _ChannelFactory.CreateChannel();
                using (_channelObject as IDisposable)
                {
                    try
                    {
                        List<Teacher> showResult = new List<Teacher>();

                        if (_channelObject != null)
                        {    
                            showResult = _channelObject.GetAllTeachers(0, _pageSize,out _teachersNumber);
                            _pageIndex = 0;
                            Bind(GridView1, showResult);
                        }

                    }

                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }

                }
                
            }
        }

        private void Bind(GridView GV, List<Teacher> table)
        {
            GV.DataSource = table;
            GV.DataBind();
        }



        protected void Button_Create_Click(object sender, EventArgs e)//call create or update page
        {
           // Response.Redirect("Create.aspx");//.............................................................................problem
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)//call update and delete 
        {
            
            
            if ("Update" == e.CommandName)
            {

                string hid = e.CommandArgument.ToString();
                Response.Redirect("UpdateTeacher.aspx?id=" + hid); 
                //Session["Id"] = hid;
                //Response.Redirect("Update.aspx");
               
            }

            if ("DeleteRow" == e.CommandName)
            {
                string hid = e.CommandArgument.ToString();
                int teacherid = Convert.ToInt32(hid);

                _channelObject = _ChannelFactory.CreateChannel();
                using (_channelObject as IDisposable)
                {
                    List<Teacher> showResult = new List<Teacher>();

                    showResult = _channelObject.GetTeacherById(teacherid);

                    foreach (var a in showResult)
                    {                   
                        
                        try
                        {
                            _channelObject.DeleteTeacherById(a.TeacherId);
                            Response.Redirect("TeacherList.aspx");
                            //Response.Write("<script>alert('Delete Succeed');window.location.href='TeacherList.aspx';</script>");
                        }

                        catch(Exception ex)
                        {
                           Response.Write("<script>alert('ex.Message');windows.location.href='TeacherList.aspx';</script>");
                        }
                        
                        Response.End();
                    }

                }
            }
        }

              

        protected void PreviousButton_Click(object sender, EventArgs e)
        {

            _channelObject = _ChannelFactory.CreateChannel();
            using (_channelObject as IDisposable)
            {
                try
                {
                    List<Teacher> showResult = new List<Teacher>();

                    if (_channelObject != null)
                    {
                        _pageIndex--;
                        if (_pageIndex < 0)
                        {
                            _pageIndex = 0;

                        }
                        showResult = _channelObject.GetAllTeachers(_pageIndex, _pageSize, out _teachersNumber);
                        Bind(GridView1, showResult);
                    }

                }

                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }

            }
        }

        protected void NextButton_Click(object sender, EventArgs e)
        {

            _channelObject = _ChannelFactory.CreateChannel();
            using (_channelObject as IDisposable)
            {
                try
                {
                    List<Teacher> showResult = new List<Teacher>();

                    if (_channelObject != null)
                    {
                        _pageIndex++;
                        showResult = _channelObject.GetAllTeachers(_pageIndex, _pageSize, out _teachersNumber);
                        Bind(GridView1, showResult);
                        if (GridView1.Rows.Count == 0)
                        {
                            _pageIndex-=1;
                            showResult = _channelObject.GetAllTeachers(_pageIndex, _pageSize, out _teachersNumber);
                            Bind(GridView1, showResult);
                        }

                    }

                }

                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }

            }
        }
    }
}